"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox ssrepair参数type无效值验证
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、修改元数据指定不存在的元数据类型
Expect      :
    1、查看成功
    2、修改失败
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.metadata = ['version', 'au_size', 'volume_count']
        self.metadata_value = [3707, 8192, 2]

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        text = '-----step2:修改元数据指定不存在的元数据类型;  expect:修改失败-----'
        self.log.info(text)

        for param in self.metadata[:3]:
            cmd1 = f"dsstbox ssrepair -v {vol_value} -t core_ctrl_wrong -k '{param}={self.metadata_value[self.metadata.index(param)]}'"
            self.log.info(cmd1)
            execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
            self.log.info(execute_cmd1)
            res1 = self.user_node.sh(execute_cmd1).result()
            self.log.info(res1)
            self.assertIn('Invalid tbox ssrepair type', res1, '修改元数据成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
